package com.mccree.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * Swagger实例Bean是Docket，所以通过配置Docket来实例配置Swagger
     * 配置swagger具体参数
     * @return
     */
    @Bean
    public Docket docket(Environment environment){
        //设置Swagger的环境
        Profiles profiles = Profiles.of("dev", "test");
        //判断是否处于该环境
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2).groupName("mccree")
                .apiInfo(apiInfo())
                .enable(flag)
                .select()//通过select方法配置扫描接口
                //RequestHandlerSelectors配置如何扫描接口
                //通过类上的注解进行扫描:withClassAnnotation(Controller.class)
                //通过方法上的注解扫描:withMethodAnnotation(GetMapping.class)
                //通过指定包扫描:basePackage("com.mccree.controller")
                .apis(RequestHandlerSelectors.basePackage("com.mccree.controller"))
                //通过path过滤
                // any()  任何请求都扫描
                // none()  任何请求都不扫描
                // regex(final String pathRegex)  通过正则表达式控制
                // ant(final String antPattern)  通过ant()控制
//                .paths(PathSelectors.ant("/mccree/**"))
                .build();
    }
    /**
     * 配置多个分组
     */
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
    }
    @Bean
    public Docket docket4(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group4");
    }
    /**
     * 配置文档信息
     */
    private ApiInfo apiInfo(){
        Contact contact = new Contact("赢旭威", "https://www.kuangstudy.com/", "jobMccree@163.com");
        return new ApiInfo(
                "Swagger学习", // 标题
                "学习演示如何配置Swagger", // 描述
                "v1.0", // 版本
                "https://www.kuangstudy.com/", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "许可链接", // 许可连接
                new ArrayList<>()// 扩展
        );
    }
}
